<?php


namespace app\controller\equipment;
require_once __DIR__.'/../../common/AuthController.php';
use app\common\AuthController;


/**
 * 设备商品
 */
class Goods extends AuthController
{   


    /**
     * 初始化操作
     */
    public function init()
    {

    }
	

    /**
     * 获取轨道商品列表
     */
	public function api_equipment_goods_getList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'code' => isset($msg['data']['code'])?$msg['data']['code']:'',
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['code'])){
            $where .= " AND equipment_code LIKE '%".$info['code']."%'";
        }
		$list = $db->query("select * from equipment_goods where ".$where ." order by id  desc limit ".$limit); 
        $count =  $db->query("SELECT count(*) as count from equipment_goods WHERE ".$where);
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
	}



    /**
     * 编辑轨道信息
     */
    public function api_equipment_goods_edit($msg)
    {   
        $info = [
            'pathway_status' => isset($msg['data']['pathway_status'])?$msg['data']['pathway_status']:'',
            'goods_id' => isset($msg['data']['goods_id'])?$msg['data']['goods_id']:'',
            'goods_code' =>  isset($msg['data']['goods_code'])?$msg['data']['goods_code']:'',
            'goods_name' => isset($msg['data']['goods_name'])?$msg['data']['goods_name']:'',
            'goods_pic' => isset($msg['data']['goods_pic'])?$msg['data']['goods_pic']:'',
            'attribute' =>  isset($msg['data']['attribute'])?$msg['data']['attribute']:'',
            'sum_number' => isset($msg['data']['sum_number'])?$msg['data']['sum_number']:'',
            'threshold' => isset($msg['data']['threshold'])?$msg['data']['threshold']:'',
            'price' => isset($msg['data']['price'])?$msg['data']['price']:'',
            'warm_time' => isset($msg['data']['warm_time'])?$msg['data']['warm_time']:'',
            'description' =>  isset($msg['data']['description'])?$msg['data']['description']:'',
            'is_discount' => isset($msg['data']['is_discount'])?$msg['data']['is_discount']:'',
        ];
        $id = isset($msg['data']['id'])?$msg['data']['id']:'';
        if(empty($id)){
            return $this->outMessage(-1,'id不能为空');
        }
        $data = [];
        foreach ($info as $k => $v) {
            if(!empty($v)){
                $data[$k] = $v;
            }
        }
        if(empty($data)){
            return $this->outMessage(10000,'保存成功');
        }
        $data['update_time'] = date('Y-m-d H:i:s',time());
        $res = $this->table->update('equipment_goods',['id'=>$id],$data);
        if(!$res){
            return $this->outMessage(-1,'保存失败');
        }
        return $this->outMessage(10000,'保存成功');
    }


    
    /**
     * 调价
     */
    public function api_equipment_goods_editPrice($msg)
    {   
        $id = isset($msg['data']['id'])?$msg['data']['id']:'';
        $price = isset($msg['data']['price'])?$msg['data']['price']:'';
        if(empty($id)){
            return $this->outMessage(-1,'id不能为空');
        }
        if(empty($price)){
            return $this->outMessage(-1,'价格不能为空');
        }
        $info = $this->table->find('equipment_goods',['id'=>$id]);
        $goods = $this->table->find('goods',['id'=>$info['goods_id']]);
        $equipment = $this->table->find('equipment_list',['code'=>$info['equipment_code']]);
        $res = $this->table->update('equipment_goods',['id'=>$id],['price'=>$price,'update_time'=>date('Y-m-d H:i:s',time())]);
        if(!$res){
            return $this->outMessage(-1,'调价失败');
        }
        //加入调价记录
        $data = [
            'equipment_id' => $equipment['id'],
            'equipment_code' => $equipment['code'],
            'category_id' => $equipment['category_id'],
            'category_name' => $equipment['category_name'],
            'goods_id' => $info['goods_id'],
            'goods_code' => $info['goods_code'],
            'goods_name' => $info['goods_name'],
            'brand_id' => $goods['brand_id'],
            'brand_name' => $goods['brand_name'],
            'attribute' => $goods['attribute'],
            'units_name' => $goods['units_name'],
            'old_price' => $info['price'],
            'new_price' => $price,
            'merchant_id' => $equipment['merchant_id'],
            'merchant_name' => $equipment['merchant_name'],
            'itoc_id' => $equipment['itoc_id'],
            'itoc_name' => $equipment['itoc_name'],
            'region_id' => $equipment['region_id'],
            'region_name' => $equipment['region_name'],
            'create_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->insert('goods_adjust_price_log',$data);
        if(!$res){
            return $this->outMessage(-1,'调价失败');
        }
        return $this->outMessage(10000,'调价成功');
    }




    /**
     * 获取调价记录列表
     */
    public function api_equipment_goods_adjustPriceList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'code' => isset($msg['data']['code'])?$msg['data']['code']:'',
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'goods_name' => isset($msg['data']['goods_name'])?$msg['data']['goods_name']:'',
            'region_name' => isset($msg['data']['region_name'])?$msg['data']['region_name']:'',
            'merchant_name' => isset($msg['data']['merchant_name'])?$msg['data']['merchant_name']:'',
            'itoc_name' => isset($msg['data']['itoc_name'])?$msg['data']['itoc_name']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['code'])){
            $where .= " AND equipment_code LIKE '%".$info['code']."%'";
        }
        if(!empty($info['goods_name'])){
            $where .= " AND goods_name LIKE '%".$info['goods_name']."%'";
        }
        if(!empty($info['region_name'])){
            $where .= " AND region_name LIKE '%".$info['region_name']."%'";
        }
        if(!empty($info['merchant_name'])){
            $where .= " AND merchant_name LIKE '%".$info['merchant_name']."%'";
        }
        if(!empty($info['itoc_name'])){
            $where .= " AND itoc_name LIKE '%".$info['itoc_name']."%'";
        }
        $list = $db->query("select * from goods_adjust_price_log where ".$where ." order by id  desc limit ".$limit); 
        $count =  $db->query("SELECT count(*) as count from goods_adjust_price_log WHERE ".$where);
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
    }



    /**
     * 添加调价申请
     */
    public function api_equipment_goods_adjustPriceAdd($msg)
    {   
        $info = [
            'merchant_circle_ids' =>  isset($msg['data']['merchant_circle_ids'])?$msg['data']['merchant_circle_ids']:'',
            'merchant_circle_name' => isset($msg['data']['merchant_circle_name'])?$msg['data']['merchant_circle_name']:'',
            'category_id' => isset($msg['data']['category_id'])?$msg['data']['category_id']:'',
            'category_name' => isset($msg['data']['category_name'])?$msg['data']['category_name']:'',
            'equipment_ids' => isset($msg['data']['equipment_ids'])?$msg['data']['equipment_ids']:'',
            'equipment_code' => isset($msg['data']['equipment_code'])?$msg['data']['equipment_code']:'',
            'equipment_count' =>  isset($msg['data']['equipment_count'])?$msg['data']['equipment_count']:'',
            'goods_count' => isset($msg['data']['goods_count'])?$msg['data']['goods_count']:'',
            'type' => isset($msg['data']['type'])?$msg['data']['type']:'',
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        $info['open_time'] = isset($msg['data']['open_time'])?$msg['data']['open_time']:''; 
        $info['adjust_reason'] = isset($msg['data']['adjust_reason'])?$msg['data']['adjust_reason']:''; 
        $list = isset($msg['data']['list'])?$msg['data']['list']:''; 
        if(!is_array($list) || empty($list)){
            return $this->outMessage(-1,'调价商品不能为空');
        }
        if($info['type'] != 1 && empty($info['open_time'])){
            return $this->outMessage(-1,'调价时间不能为空');
        }
        if(!empty($info['adjust_reason'])){
            if(!$this->validate->checkName($info['adjust_reason'],2,100)){
                return $this->outMessage(-1,'请输入正确的调价理由');
            }
        }
        $this->admin_id = 1;
        $admin_info = $this->table->find('admin',['id'=>$this->admin_id]);
        $data = [
            'order_no' => $this->common->createOutTradeNo('T'),
            'itoc_id' => $admin_info['itoc_id'],
            'itoc_name' => $admin_info['itoc_name'],
            'proposer_id' => $this->admin_id,
            'proposer_nickname' => $admin_info['nickname'],
            'merchant_circle_ids' => $info['merchant_circle_ids'],
            'merchant_circle_name' => $info['merchant_circle_name'],
            'category_id' => $info['category_id'],
            'category_name' => $info['category_name'],
            'equipment_ids' => $info['equipment_ids'],
            'equipment_code' => $info['equipment_code'],
            'equipment_count' => $info['equipment_count'],
            'goods_count' => $info['goods_count'],
            'adjust_reason' => $info['adjust_reason'],
            'type' => $info['type'],
            'open_time' => $info['open_time'],
            'create_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->insert('goods_adjust_price',$data);
        if(!$res){
            return $this->outMessage(-1,'提交失败');
        }
        //详细信息
        foreach ($list as $v) {
            $goods = $this->table->find('goods',['id'=>$v['id']]);
            $goods_data = [
                'order_no' => $data['order_no'],
                'goods_id' => $v['id'],
                'goods_code' => $goods['code'],
                'goods_name' => $goods['name'],
                'brand_id' => $goods['brand_id'],
                'brand_name' => $goods['brand_name'],
                'attribute' => $goods['attribute'],
                'units_name' => $goods['units_name'],
                'old_price' => $goods['price'],
                'new_price' => $v['new_price'],
                'create_time' => date('Y-m-d H:i:s',time())
            ];
            $res = $this->table->insert('goods_adjust_price_list',$goods_data);
            if(!$res){
                return $this->outMessage(-1,'提交失败');
            }
        }
        return $this->outMessage(10000,'提交成功');
    }



    /**
     * 获取调价申请列表
     */
    public function api_equipment_goods_itocAdjustPriceList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'order_no' => isset($msg['data']['order_no'])?$msg['data']['order_no']:'',
            'itoc_name' => isset($msg['data']['itoc_name'])?$msg['data']['itoc_name']:'',
            'status' => isset($msg['data']['status'])?$msg['data']['status']:'',
            'type' => isset($msg['data']['type'])?$msg['data']['type']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['status'])){
            $where .= " AND status =".$info['status'];
        }
        if(!empty($info['type'])){
            $where .= " AND type =".$info['type'];
        }
        if(!empty($info['order_no'])){
            $where .= " AND order_no LIKE '%".$info['order_no']."%'";
        }
        if(!empty($info['itoc_name'])){
            $where .= " AND itoc_name LIKE '%".$info['itoc_name']."%'";
        }
        $list = $db->query("select * from goods_adjust_price where ".$where ." order by id  desc limit ".$limit); 
        $count =  $db->query("SELECT count(*) as count from goods_adjust_price WHERE ".$where);
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
    }



    /**
     * 获取调价申请列表
     */
    public function api_equipment_goods_itocAdjustPriceDetails($msg)
    {   
        $order_no = isset($msg['data']['order_no'])?$msg['data']['order_no']:'';
        if(empty($order_no)){
            return $this->outMessage(-1,'申请单号不能为空');
        }
        $list = $this->table->select('goods_adjust_price_list',['order_no'=>$order_no]);
        return $this->outMessage(10000,'获取成功',$list);
    }



    /**
     * 审核
     */
    public function api_equipment_goods_itocAdjustPriceCheck($msg)
    {   
        global $db;
        $id = isset($msg['data']['id'])?$msg['data']['id']:'';
        $status = isset($msg['data']['status'])?$msg['data']['status']:'';
        $check_remark = isset($msg['data']['check_remark'])?$msg['data']['check_remark']:'';
        if(empty($id) || empty($status)){
            return $this->outMessage(-1,'参数不能为空');
        }
        $data = [
            'status' => $status,
            'check_admin_id' => $this->admin_id,
            'check_remark' => $check_remark,
            'check_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->update('goods_adjust_price',['id'=>$id],$data);
        if(!$res){
            return $this->outMessage(-1,'保存失败');
        }
        //修改商品信息
        $info = $this->table->find('goods_adjust_price',['id'=>$id]);
        //类型为立即生效
        if($status == 2 && $info['type'] == 1){
            $list = $this->table->select('goods_adjust_price_list',['order_no'=>$info['order_no']]);
            foreach ($list as $v) {
                $res = $db->query("UPDATE equipment_goods SET price = ".$v['new_price']." WHERE goods_id = ".$v['goods_id'] ." AND equipment_id in (".$info['equipment_ids'].")");
            }

        }
        return $this->outMessage(10000,'保存成功');
    }



    /**
     * 价格异常报表
     */
    public function api_equipment_goods_priceUnusual($msg)
    {
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'itoc_name' => isset($msg['data']['itoc_name'])?$msg['data']['itoc_name']:'',
            'merchant_name' => isset($msg['data']['merchant_name'])?$msg['data']['merchant_name']:'',
            'goods_name' => isset($msg['data']['goods_name'])?$msg['data']['goods_name']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = " AND a.is_delete = 0";
        if(!empty($info['itoc_name'])){
            $where .= " AND b.itoc_name LIKE '%".$info['itoc_name']."%'";
        }
        if(!empty($info['merchant_name'])){
            $where .= " AND b.merchant_name LIKE '%".$info['merchant_name']."%'";
        }
        if(!empty($info['goods_name'])){
            $where .= " AND a.goods_name LIKE '%".$info['goods_name']."%'";
        }
        $list = $db->query("SELECT b.itoc_name,b.merchant_name,a.equipment_code,c.category_name_2,a.goods_code,a.goods_name,c.buy_price,a.price,a.create_time FROM equipment_goods as a left join equipment_list as b on a.equipment_code = b.code left join goods as c on a.goods_id = c.id WHERE a.price < c.buy_price*0.8 or a.price > c.buy_price*3 ".$where." GROUP BY a.equipment_code,a.goods_id limit ".$limit);
        $count =  $db->query("SELECT b.itoc_name,b.merchant_name,a.equipment_code,c.category_name_2,a.goods_code,a.goods_name,c.buy_price,a.price,a.create_time FROM equipment_goods as a left join equipment_list as b on a.equipment_code = b.code left join goods as c on a.goods_id = c.id WHERE a.price < c.buy_price*0.8 or a.price > c.buy_price*3 ".$where." GROUP BY a.equipment_code,a.goods_id");
        return $this->outMessage(10000,'获取成功',$list,'success',count($count));
    }

}
